{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.导入数据（10分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = load_iris()\n",
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.切分数据集（10分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=1/3,random_state=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 0, 0, 2, 2, 2, 0, 2, 0, 1, 0, 1, 1, 1, 0, 1, 2, 1, 2, 0, 1,\n",
       "       2, 2, 0, 0, 0, 0, 1, 2, 0, 1, 0, 1, 1, 2, 2, 2, 0, 2, 0, 1, 0, 2,\n",
       "       2, 1, 2, 1, 2, 0])"
      ]
     },
     "execution_count": 225,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化（20分）\n",
    "分别对训练集和测试集的特征值做标准化,计算公式如下:\n",
    "![](https://pic3.zhimg.com/80/v2-9afbefb7425827f474731bd38f0d427e_720w.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = StandardScaler().fit_transform(X_train)\n",
    "X_test = StandardScaler().fit_transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.52297636,  0.81099822,  0.48973036,  0.50870336],\n",
       "       [ 0.63192977, -0.92685511,  0.81914541,  0.8940847 ],\n",
       "       [-0.89341795, -0.05792844, -1.21224736, -1.28974287],\n",
       "       [-0.67551113,  1.10064045, -1.32205237, -1.28974287],\n",
       "       [ 2.04832408, -1.21649734,  1.69758552,  1.40792647],\n",
       "       [ 0.95878999,  0.81099822,  1.03875544,  1.15100558],\n",
       "       [ 2.04832408, -0.05792844,  1.25836547,  1.40792647],\n",
       "       [-0.45760432,  1.96956712, -1.26714987, -1.28974287],\n",
       "       [ 0.63192977, -0.63721289,  0.98385293,  1.15100558],\n",
       "       [-1.43818499, -2.08542401, -1.37695488, -1.16128242],\n",
       "       [-0.13074409, -0.63721289,  0.37992535,  0.12332203],\n",
       "       [-1.65609181, -0.05792844, -1.4867599 , -1.41820331],\n",
       "       [ 0.63192977,  0.521356  ,  0.37992535,  0.38024292],\n",
       "       [ 0.30506954, -0.34757067,  0.48973036,  0.25178247],\n",
       "       [ 0.41402295, -0.34757067,  0.27012034,  0.12332203],\n",
       "       [-0.45760432,  1.10064045, -1.15734485, -1.28974287],\n",
       "       [ 0.95878999,  0.23171378,  0.32502284,  0.25178247],\n",
       "       [ 0.08716273, -0.05792844,  0.70934039,  0.76562425],\n",
       "       [-0.89341795, -2.95435068, -0.16909972, -0.26205931],\n",
       "       [ 0.41402295, -0.63721289,  0.54463287,  0.76562425],\n",
       "       [-1.11132477,  1.10064045, -1.04753984, -1.28974287],\n",
       "       [ 0.84983659, -0.05792844,  0.32502284,  0.25178247],\n",
       "       [-0.02179068, -0.92685511,  0.70934039,  0.8940847 ],\n",
       "       [ 2.04832408, -0.63721289,  1.58778051,  1.02254514],\n",
       "       [-1.32923158,  1.67992489, -1.5416624 , -1.28974287],\n",
       "       [-1.00237136,  0.23171378, -1.26714987, -1.41820331],\n",
       "       [-0.89341795,  1.10064045, -1.26714987, -1.28974287],\n",
       "       [-1.32923158,  0.521356  , -1.32205237, -1.28974287],\n",
       "       [-0.67551113, -0.92685511,  0.05051031,  0.25178247],\n",
       "       [ 0.63192977, -0.63721289,  0.98385293,  1.27946603],\n",
       "       [-1.11132477, -0.05792844, -1.32205237, -1.16128242],\n",
       "       [ 0.08716273, -0.05792844,  0.21521783,  0.38024292],\n",
       "       [-0.89341795,  1.10064045, -1.21224736, -1.03282198],\n",
       "       [-1.00237136, -1.79578178, -0.27890474, -0.26205931],\n",
       "       [-0.2396975 , -0.05792844,  0.37992535,  0.38024292],\n",
       "       [ 1.50355704, -0.05792844,  1.09365794,  0.50870336],\n",
       "       [ 1.72146385, -0.63721289,  1.25836547,  0.8940847 ],\n",
       "       [ 1.0677434 ,  0.521356  ,  1.14856045,  1.40792647],\n",
       "       [-1.11132477,  1.10064045, -1.21224736, -1.28974287],\n",
       "       [ 0.30506954, -1.21649734,  0.98385293,  0.25178247],\n",
       "       [-0.56655772,  1.96956712, -1.26714987, -1.28974287],\n",
       "       [ 0.19611614, -0.34757067,  0.37992535,  0.38024292],\n",
       "       [-1.5471384 , -0.05792844, -1.37695488, -1.28974287],\n",
       "       [-1.00237136, -1.50613956,  0.37992535,  0.63716381],\n",
       "       [ 0.74088318,  0.521356  ,  0.70934039,  1.02254514],\n",
       "       [-0.13074409, -0.05792844,  0.21521783, -0.00513842],\n",
       "       [ 1.17669681,  0.521356  ,  1.03875544,  1.40792647],\n",
       "       [ 0.95878999, -0.05792844,  0.65443789,  0.63716381],\n",
       "       [ 0.41402295,  1.10064045,  0.87404792,  1.40792647],\n",
       "       [-0.78446454,  1.10064045, -1.26714987, -1.28974287]])"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳模型参数的评分: 0.97\n",
      "最优参数:\n",
      "{'C': 0.525, 'class_weight': None, 'dual': False, 'fit_intercept': True, 'intercept_scaling': 1, 'l1_ratio': None, 'max_iter': 100, 'multi_class': 'auto', 'n_jobs': None, 'penalty': 'l2', 'random_state': None, 'solver': 'sag', 'tol': 0.0001, 'verbose': 0, 'warm_start': False}\n"
     ]
    }
   ],
   "source": [
    "param_grid = {\n",
    "    'C':list(np.linspace(0.05,1,19)),\n",
    "    'solver':['liblinear','sag','newton-cg','lbfgs']\n",
    "}\n",
    "\n",
    "gsc = GridSearchCV(LogisticRegression(),param_grid)\n",
    "#print(x_train)\n",
    "gsc.fit(X_train,y_train)\n",
    "\n",
    "print('最佳模型参数的评分:',gsc.best_score_)\n",
    "print('最优参数:')\n",
    "best_params = gsc.best_estimator_.get_params()\n",
    "print(best_params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.将最优的结果重新用来实例化模型，查看训练集和测试集下的分数（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.525, solver='sag')"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = LogisticRegression(penalty='l2',\n",
    "           max_iter=best_params['max_iter'],\n",
    "           C=best_params['C'],\n",
    "           solver=best_params['solver'])\n",
    "model.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在训练集上的准确度评分: 0.97\n",
      "在测试集上的准确度评分: 0.92\n"
     ]
    }
   ],
   "source": [
    "#模型评分 准确率\n",
    "s1 = model.score(X_train,y_train)\n",
    "s2 = model.score(X_test,y_test)\n",
    "print('在训练集上的准确度评分:',s1)\n",
    "print('在测试集上的准确度评分:',s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.计算召回率、AUC（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用测试集计算召回率: 0.92\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import recall_score, roc_auc_score\n",
    "print('使用测试集计算召回率:', recall_score(y_test, model.predict(X_test), average='micro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用测试集计算AUC: 0.9894874338624339\n"
     ]
    }
   ],
   "source": [
    "print('使用测试集计算AUC:', roc_auc_score(y_test, model.predict_proba(X_test), multi_class='ovr'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
